﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Forms.Local
{
    /// <summary>
    /// 基于直连数据的的表格控件。
    /// </summary>
    [DefaultEvent("AfterLoad")]
    public partial class DataGrid : Marmat.Forms.DataGrid
    {
        private bool m_Loading = false;

        /// <summary>
        /// 初始化 Marmat.Forms.Local.DataGrid 类的新实例。
        /// </summary>
        public DataGrid()
        {
            InitializeComponent();
            this.pageControl1.SendToBack();
            this.pageControl1.LoadPage = this.LoadPage;
            this.pageControl1.LoadTop = this.LoadTop;
            this.CurrentChanged += this.DataGrid_CurrentChanged;
        }

        private void DataGrid_CurrentChanged(object sender, Views.DataGrid.GridEventArgs e)
        {
            if (this.m_Loading == false)
            {
                this.UpdateRelation();
            }
        }

        private void pageControl1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Rectangle rect = new Rectangle(0, 0, pageControl1.Width - 1, pageControl1.Height - 1);
            using (Pen Pen = new Pen(this.BaseControl.BackColor))
            {
                e.Graphics.DrawRectangle(Pen, rect);
            }
        }

        private void pageControl1_ItemClick(object sender, EventArgs e)
        {
            this.LoadPage = this.pageControl1.LoadPage;
            this.LoadTop = this.pageControl1.LoadTop;
            this.Load();
        }

        /// <summary>
        /// 将 SaveProjects 属性重置为其默认值。
        /// </summary>
        protected void ResetSaveProjects()
        {
            this.SaveProjects.Clear();
        }

        /// <summary>
        /// 是否应该序列化 SaveProjects 属性。
        /// </summary>
        /// <returns></returns>
        protected bool ShouldSerializeSaveProjects()
        {
            return !(this.SaveProjects.Count == 0);
        }

        /// <summary>
        /// 将 Relation 属性重置为其默认值。
        /// </summary>
        protected void ResetRelation()
        {
            this.Relation.Items.Clear();
            this.Relation.SyncLoadMode = SyncLoadModeEnum.Load;
            this.Relation.Table = null;
        }

        /// <summary>
        ///     ''' 是否应该序列化 Relation 属性。
        ///     ''' </summary>
        ///     ''' <returns></returns>
        protected bool ShouldSerializeRelation()
        {
            return !(this.Relation.Items.Count == 0 && this.Relation.SyncLoadMode == SyncLoadModeEnum.None && this.Relation.Table == null); // AndAlso Me.Relation.Update = False
        }
    }
}
